package org.dioib.practice.common.constant;

import org.springframework.lang.Nullable;

public enum StatusCode {
    SUCCESS(Serial.OK,200,  "操作成功"),
    NOT_FOUND(Serial.OK,404, "资源不存在"),
    INTERNAL_ERROR(Serial.OK,500, "服务器内部错误"),
    PARAM_INVALID(Serial.OK,1001, "参数无效"),
    PASSWORD_ERROR(Serial.OK,500, "密码错误"),
    USER_NOT_FOUND(Serial.OK,2001, "用户不存在");

    private Serial serial;
    private int code;
    private String desc;

    StatusCode(Serial serial, int code, String desc) {
        this.code = code;
        this.serial = serial;
        this.desc = desc;
    }

    public int getCode() {
        return code;
    }

    public Serial getSerial() {
        return serial;
    }

    public String getDesc() {
        return desc;
    }

    public static StatusCode valueOf(int statusCode) {
        StatusCode status = resolve(statusCode);
        if (status == null) {
            throw new IllegalArgumentException("No matching constant for [" + statusCode + "]");
        }
        return status;
    }

    @Nullable
    public static StatusCode resolve(int statusCode) {
        // Use cached VALUES instead of values() to prevent array allocation.
        for (StatusCode status : values()) {
            if (status.getCode() == statusCode) {
                return status;
            }
        }
        return null;
    }
}
